package shared.util;

import shared.other.TimePeriod;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtils {
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public static String getMonthResourceKey(int month) {
        switch (month) {
            case Calendar.JANUARY:
                return "calendar.month.january";
            case Calendar.FEBRUARY:
                return "calendar.month.february";
            case Calendar.MARCH:
                return "calendar.month.march";
            case Calendar.APRIL:
                return "calendar.month.april";
            case Calendar.MAY:
                return "calendar.month.may";
            case Calendar.JUNE:
                return "calendar.month.june";
            case Calendar.JULY:
                return "calendar.month.july";
            case Calendar.AUGUST:
                return "calendar.month.august";
            case Calendar.SEPTEMBER:
                return "calendar.month.september";
            case Calendar.OCTOBER:
                return "calendar.month.october";
            case Calendar.NOVEMBER:
                return "calendar.month.november";
            case Calendar.DECEMBER:
                return "calendar.month.december";
        }
        return String.valueOf(month);
    }

    public static String getDayOfWeekResourceKey(int dayOfWeek) {
        switch (dayOfWeek) {
            case Calendar.MONDAY:
                return "calendar.week.monday";
            case Calendar.TUESDAY:
                return "calendar.week.tuesday";
            case Calendar.WEDNESDAY:
                return "calendar.week.wednesday";
            case Calendar.THURSDAY:
                return "calendar.week.thursday";
            case Calendar.FRIDAY:
                return "calendar.week.friday";
            case Calendar.SATURDAY:
                return "calendar.week.saturday";
            case Calendar.SUNDAY:
                return "calendar.week.sunday";
        }
        return String.valueOf(dayOfWeek);
    }

    public static Date getFirstDayOfTheMonthAsDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static String getTodayDateAsString() {
        return dateFormat.format(new Date());
    }

    public static String formatDate(Date date) {
        return dateFormat.format(date);
    }

    public static boolean isDateInRange(Date date, TimePeriod range) {
        if (date.after(range.getEndDate()) || date.before(range.getStartDate())) {
            return false;
        }

        return true;

    }

    public static boolean areTimePeriodOverlapping(TimePeriod period1, TimePeriod period2) {
        if (isDateInRange(period1.getStartDate(), period2) || isDateInRange(period1.getEndDate(), period2)) {
            return true;
        }

        return false;

    }

    public static boolean isWeekendDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
            return true;
        }
        return false;
    }

    public static String appendCurrentTime(String inputString) {
        String currentTime = timeFormat.format(new Date());
        return "[" + currentTime + "] " + inputString;
    }
}


